Skip to content

fix: cache default location safely - #37

Open
pragnyanramtha wants to merge 3 commits into
imrohan18:mainfrom
pragnyanramtha:fix-threadsafe-location-cache
Open

fix: cache default location safely#37
pragnyanramtha wants to merge 3 commits into
imrohan18:mainfrom
pragnyanramtha:fix-threadsafe-location-cache

Conversation

@pragnyanramtha

Copy link
Copy Markdown

Summary

  • remove mutable global IP location cache state from the medicines router
  • cache successful IP location lookups with a single-entry lru_cache
  • keep fallback behavior retryable when the IP lookup fails
  • add focused backend tests for cache behavior and fallback behavior

Fixes #33

Validation

  • python3 -m unittest discover backend/tests -v
  • python3 -m py_compile backend/routers/medicines.py backend/tests/test_medicines.py
  • git diff --check

Copilot AI review requested due to automatic review settings May 19, 2026 17:09
@vercel

vercel Bot commented May 19, 2026

Copy link
Copy Markdown

@pragnyanramtha is attempting to deploy a commit to the rohanhake256's projects Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses issue #33 by replacing the previous mutable module-level IP location cache in backend/routers/medicines.py with a single-entry functools.lru_cache, while keeping the fallback default location retryable after failures. It also adds backend unit tests to validate caching and fallback behavior.

Changes:

  • Removed mutable global IP location cache state and introduced DEFAULT_LOCATION plus an @lru_cache(maxsize=1)-backed _fetch_ip_location().
  • Updated get_default_location() to return cached IP-based coordinates on success and fall back to DEFAULT_LOCATION on failure.
  • Added unit tests covering (1) caching success and (2) retry behavior after an initial failure.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
backend/routers/medicines.py Replaces mutable globals with an lru_cache-based IP lookup and a named default fallback.
backend/tests/test_medicines.py Adds unit tests to assert caching behavior and failure fallback/retry behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backend/tests/test_medicines.py
Comment thread backend/tests/test_medicines.py Outdated
Comment thread backend/routers/medicines.py Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Backend uses non-thread-safe global variables for location caching

2 participants